﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Autorent.Desktop.Main;
using Autorent.Desktop.Views;
using Autorent.Desktop.Model.Services;

namespace Autorent.Desktop.ViewModels
{
    public class LoginViewModel
    {
        #region Public Properties

        public LoginView View { get; set; }

        #endregion

        #region Constructors

        public LoginViewModel(LoginView view)
        {
            View = view;
            View.Model = this;
        }

        #endregion

        #region Commands

        public ICommand Login
        {
            get
            {
                return new RelayCommand(param => login());
            }
        }

        public ICommand Cancel
        {
            get
            {
                return new RelayCommand(param => cancel());
            }
        }

        #endregion

        #region Private Methods

        private void login()
        {
            string userName = View.txtBoxLogin.Text;
            string password = View.txtBoxPassword.Password;

            if (User.Validate(userName, password))
            {
                User.SetCredentials(userName, password);

                View.DialogResult = true;
            }
            else
            {
                MessageBox.Show("Wrong login or password!", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void cancel()
        {
            Application.Current.Shutdown();
        }

        #endregion
    }
}
